package com.health.platform.provider.diseasemonitoring.entity;

import com.health.platform.common.utils.ThreadLocalUtil;
import org.springframework.util.StringUtils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.WeekFields;
import com.health.platform.common.utils.MyUtils;

/**
 * <p>传染病-实体类</p>
 * <p>创建日期：2021-02-26</p>
 *
 * @author wangyanlei
 */
public class InfectionEntity {

    /**
     * 区划编号
     */
    private String sgbCode;
    /**
     * 指标类型数组
     */
    private String[] targetTypeArr;
    /**
     * 时间分组数组
     */
    private String[] recentYearArr;
    /**
     * 去年同期时间分组数组
     */
    private String[] recentYearCompareArr;
    /**
     * 是否计算页面选择时间的上一年同期
     * 默认是false 不计算
     */
    private boolean compare;
    /**
     * 类型
     */
    private String targetType;
    /**
     * 传染病代码
     */
    private String infectionCode;
    /**
     * 区域构建体
     */
    private StringBuilder paramAreaBuilder = new StringBuilder();
    /**
     * 日期构建体
     */
    private StringBuilder paramDateBuilder = new StringBuilder();
    /**
     * 单纯为获取同比
     */
    private StringBuilder paramDateCompareBuilder = new StringBuilder();
    /**
     * 单纯为折线图
     */
    private StringBuilder paramDateLineBuilder = new StringBuilder();
    /**
     * 折线图同期条件
     */
    private StringBuilder paramDateLineCompareBuilder = new StringBuilder();
    /**
     * 统一条件拼接
     */
    private String conditionAreaStr;
    /**
     * 页面选择时间SQl拼接条件
     */
    private String conditionDateStr;
    /**
     * 页面选择时间的上一年同期SQl拼接条件
     */
    private String conditionCompareDateStr;
    /**
     * 折线图需要的时间条件
     */
    private String conditionLineDateStr;
    /**
     * 折线图同期条件
     */
    private String conditionLineCompareDateStr;
    /**
     * 日期参数
     * 1 今日
     * 2 上一日
     * 3 近一周
     * 4 近一月
     * 5 上月
     * 6 本月
     * 7 近一年
     * 8 上年
     * 9 本年
     */
    private Integer dateType;

    /**
     * 自定义开始日期
     */
    private String startDate;

    /**
     * 自定义结束日期
     */
    private String endDate;

    /**
     * 折线图选择的时间
     */
    private Integer lineType;
    /**
     * 时间格式化格式
     */
    private String dateFormat;

    // 区域
    public void setSgbCode(String sgbCode) {
        this.sgbCode = sgbCode;
    }
    public void setStartDate(String startDate) {
        this.startDate = startDate;
        if(StringUtils.hasText(startDate)){
            DateTimeFormatter fmDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate parse = LocalDate.parse(startDate, fmDate);
            //去年今天
            LocalDate dateLast = parse.minusYears(1);
            String dateLastStr = dateLast.format(fmDate);
            paramDateBuilder.append("and to_char(t.bus_date, 'yyyy-MM-dd') >='" + startDate + "'");
            paramDateCompareBuilder.append("and to_char(t.bus_date, 'yyyy-MM-dd')>='" + dateLastStr + "'");
        }
    }
    public void setEndDate(String endDate) {
        this.endDate = endDate;
        if (StringUtils.hasText(endDate)) {
            //判断当前传入的结束时间日期是否在6.1号以前，在6.1以前当前年份为前一年，在6.1以后当前年份取结束时间年份
            DateTimeFormatter fmDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate parse = LocalDate.parse(endDate, fmDate);
            //去年今天
            LocalDate dateLast = parse.minusYears(1);
            String dateLastStr = dateLast.format(fmDate);
            // 清空
            paramDateBuilder.append("and to_char(t.bus_date, 'yyyy-MM-dd') <='" + endDate + "'");
            paramDateCompareBuilder.append("and to_char(t.bus_date, 'yyyy-MM-dd')<='" + dateLastStr + "'");

        }
    }
    /**
     * 设置日期参数
     * 1 今日
     * 2 上一日
     * 5 上月
     * 6 本月
     * 8 上年
     * 9 本年
     */
    // 时间
    public void setDateType(Integer dateType) {
        this.dateType = dateType;
        String dateStr = null;
        String dateLastStr = null;
        if(dateType == null ){
            return;
        }
        // 月格式化
        DateTimeFormatter monthDate = DateTimeFormatter.ofPattern("yyyy-MM");
        DateTimeFormatter fmDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (1 == dateType) {
            //当天
            dateStr = LocalDate.now().format(fmDate);
            dateLastStr = LocalDate.now().plus(-1, ChronoUnit.YEARS).format(fmDate);
            // 清空
            paramDateBuilder.delete(0, paramDateBuilder.length());
            paramDateBuilder.append("and to_char(t.bus_date, 'yyyy-MM-dd')='" + dateStr + "'");
            paramDateCompareBuilder.append("and to_char(t.bus_date, 'yyyy-MM-dd')='" + dateLastStr + "'");

        } else if (2 == dateType) {
            // 上一日
            dateStr = LocalDate.now().minus(1, ChronoUnit.DAYS).format(fmDate);
            dateLastStr = LocalDate.now().minus(1, ChronoUnit.DAYS).plus(-1, ChronoUnit.YEARS).format(fmDate);
            paramDateBuilder.delete(0, paramDateBuilder.length());
            paramDateBuilder.append("and to_char(t.bus_date, 'yyyy-MM-dd')='" + dateStr + "'");
            paramDateCompareBuilder.append("and to_char(t.bus_date, 'yyyy-MM-dd')='" + dateLastStr + "'");
        } else if (6 == dateType) {
            // 本月
            dateStr = LocalDate.now().minusMonths(0).format(monthDate);
            dateLastStr = LocalDate.now().minusMonths(0).plus(-1, ChronoUnit.YEARS).format(monthDate);
            paramDateBuilder.delete(0, paramDateBuilder.length());
            paramDateBuilder.append("and to_char(t.bus_date, 'yyyy-MM')='" + dateStr + "'");
            paramDateCompareBuilder.append("and to_char(t.bus_date, 'yyyy-MM')='" + dateLastStr + "'");
        } else if (5 == dateType) {
            // 上月
            dateStr = LocalDate.now().minusMonths(1).format(monthDate);
            dateLastStr = LocalDate.now().minusMonths(1).plus(-1, ChronoUnit.YEARS).format(monthDate);
            paramDateBuilder.append("and to_char(t.bus_date, 'yyyy-MM')='" + dateStr + "'");
            paramDateCompareBuilder.append("and to_char(t.bus_date, 'yyyy-MM')='" + dateLastStr + "'");
        } else if (8 == dateType) {
            // 上年
            dateStr = String.valueOf(LocalDate.now().getYear() -1);
            dateLastStr = String.valueOf(LocalDate.now().getYear() - 2);
            paramDateBuilder.append("and to_char(t.bus_date, 'yyyy')='" + dateStr + "'");
            paramDateCompareBuilder.append("and to_char(t.bus_date, 'yyyy')='" + dateLastStr + "'");
        } else if (9 == dateType) {
            //   本年
            dateStr = String.valueOf(LocalDate.now().getYear() );
            dateLastStr = String.valueOf(LocalDate.now().getYear() - 1);
            paramDateBuilder.append("and to_char(t.bus_date, 'yyyy')='" + dateStr + "'");
            paramDateCompareBuilder.append("and to_char(t.bus_date, 'yyyy')='" + dateLastStr + "'");
        }
    }

    /**
     * 1、按日<近7日>、
     * 2、周<近12周>、
     * 3、月<近12个月>、
     * 4、年<近5年
     */
    public void setLineType(Integer lineType) {
        this.lineType = lineType;
        String dateStr = null;
        String dateLastStr=null;
        DateTimeFormatter fmDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter monthDate = DateTimeFormatter.ofPattern("yyyy-MM");
        if (1 == lineType) {
            //  1 按日--近7日
            //当前
            dateStr = LocalDate.now().format(fmDate);
            String dateStr1 = LocalDate.now().minus(1, ChronoUnit.DAYS).format(fmDate);
            String dateStr2 = LocalDate.now().minus(2, ChronoUnit.DAYS).format(fmDate);
            String dateStr3 = LocalDate.now().minus(3, ChronoUnit.DAYS).format(fmDate);
            String dateStr4 = LocalDate.now().minus(4, ChronoUnit.DAYS).format(fmDate);
            String dateStr5 = LocalDate.now().minus(5, ChronoUnit.DAYS).format(fmDate);
            String dateStr6 = LocalDate.now().minus(6, ChronoUnit.DAYS).format(fmDate);
            // 去年同期
            LocalDate dateLast=LocalDate.now().plus(-1, ChronoUnit.YEARS);
            dateLastStr = dateLast.format(fmDate);
            String dateLastStr1 = dateLast.minus(1, ChronoUnit.DAYS).format(fmDate);
            String dateLastStr2 = dateLast.minus(2, ChronoUnit.DAYS).format(fmDate);
            String dateLastStr3 = dateLast.minus(3, ChronoUnit.DAYS).format(fmDate);
            String dateLastStr4 = dateLast.minus(4, ChronoUnit.DAYS).format(fmDate);
            String dateLastStr5 = dateLast.minus(5, ChronoUnit.DAYS).format(fmDate);
            String dateLastStr6 = dateLast.minus(6, ChronoUnit.DAYS).format(fmDate);
            recentYearArr = new String[]{dateStr6,dateStr5,dateStr4,dateStr3,dateStr2,dateStr1,dateStr};
            recentYearCompareArr = new String[]{dateLastStr6,dateLastStr5,dateLastStr4,dateLastStr3,dateLastStr2,dateLastStr1,dateLastStr};
            dateStr="('"+dateStr+"','"+dateStr1+"','"+dateStr2+"','"+dateStr3+"','"+dateStr4+"','"+dateStr5+"','"+dateStr6+"')";
            dateLastStr="('"+dateLastStr+"','"+dateLastStr1+"','"+dateLastStr2+"','"+dateLastStr3+"','"+dateLastStr4+"','"+dateLastStr5+"','"+dateLastStr6+"')";
            paramDateLineBuilder.append("and to_char(t.bus_date, 'yyyy-MM-dd') in " + dateStr + "");
            paramDateLineCompareBuilder.append("and to_char(t.bus_date, 'yyyy-MM-dd') in " + dateLastStr + "");
            this.dateFormat="yyyy-MM-dd";
        }else if(2 == lineType){
            // 本年
            String nowYear = String.valueOf(LocalDate.now().getYear());
            // 去年同期
            String lastYear = String.valueOf(LocalDate.now().getYear() - 1);
            //   2 按周--近12周
            LocalDate localDate1 = LocalDate.now();
            WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY,4);
            //获取当前时间在当前年的第几周
            int week= localDate1.get(weekFields.weekOfWeekBasedYear());
            //    获取近12周 如果小于12 那么有几周算几周
            String dateStr1 = week >= 10 ? ""+week : "0"+week;
            String dateStr2 = (week-1) >= 10 ? ""+(week-1) : (week-1) <= 0 ?"0":"0"+(week-1);
            String dateStr3 = (week-2) >= 10 ? ""+(week-2) : (week-2) <= 0 ?"0":"0"+(week-2);
            String dateStr4 = (week-3) >= 10 ? ""+(week-3) : (week-3) <= 0 ?"0":"0"+(week-3);
            String dateStr5 = (week-4) >= 10 ? ""+(week-4) : (week-4) <= 0 ?"0":"0"+(week-4);
            String dateStr6 = (week-5) >= 10 ? ""+(week-5) : (week-5) <= 0 ?"0":"0"+(week-5);
            String dateStr7 = (week-6) >= 10 ? ""+(week-6) : (week-6) <= 0 ?"0":"0"+(week-6);
            String dateStr8 = (week-7) >= 10 ? ""+(week-7) : (week-7) <= 0 ?"0":"0"+(week-7);
            String dateStr9 = (week-8) >= 10 ? ""+(week-8) : (week-8) <= 0 ?"0":"0"+(week-8);
            String dateStr10 = (week-9) >= 10 ? ""+(week-9) : (week-9) <= 0 ?"0":"0"+(week-9);
            String dateStr11 = (week-10) >= 10 ? ""+(week-10) : (week-10) <= 0 ?"0":"0"+(week-10);
            String dateStr12 = (week-11) >= 10 ? ""+(week-11) : (week-11) <= 0 ?"0":"0"+(week-11);
            recentYearArr = new String[]{dateStr12,dateStr11,dateStr10,dateStr9,dateStr8,dateStr7,dateStr6,dateStr5,dateStr4,dateStr3,dateStr2,dateStr1};
            recentYearCompareArr = new String[]{dateStr12,dateStr11,dateStr10,dateStr9,dateStr8,dateStr7,dateStr6,dateStr5,dateStr4,dateStr3,dateStr2,dateStr1};
            dateStr="('"+dateStr1+"','"+dateStr2+"','"+dateStr3+"','"+dateStr4+"','"+dateStr5+"','"+dateStr6+"','"+dateStr7+"','"+dateStr8+"','"+dateStr9+"','"+dateStr10+"','"+dateStr11+"','"+dateStr12+"')";
            paramDateLineBuilder.append("and to_char(t.bus_date, 'yyyy') = '"+nowYear+"' and to_char(t.bus_date, 'WW') in " + dateStr + "");
            paramDateLineCompareBuilder.append("and to_char(t.bus_date, 'yyyy') = '"+lastYear+"' and to_char(t.bus_date, 'WW') in " + dateStr + "");
            this.dateFormat="WW";
        }else if(3 == lineType){
            //  3 按月--近12个月
            // 本月
            dateStr = LocalDate.now().minusMonths(0).format(monthDate);
            String dateStr1 = LocalDate.now().minusMonths(1).format(monthDate);
            String dateStr2 = LocalDate.now().minusMonths(2).format(monthDate);
            String dateStr3 = LocalDate.now().minusMonths(3).format(monthDate);
            String dateStr4 = LocalDate.now().minusMonths(4).format(monthDate);
            String dateStr5 = LocalDate.now().minusMonths(5).format(monthDate);
            String dateStr6 = LocalDate.now().minusMonths(6).format(monthDate);
            String dateStr7 = LocalDate.now().minusMonths(7).format(monthDate);
            String dateStr8 = LocalDate.now().minusMonths(8).format(monthDate);
            String dateStr9 = LocalDate.now().minusMonths(9).format(monthDate);
            String dateStr10 = LocalDate.now().minusMonths(10).format(monthDate);
            String dateStr11 = LocalDate.now().minusMonths(11).format(monthDate);
            // 去年同期
            LocalDate dateLast=LocalDate.now().minusMonths(0).plus(-1, ChronoUnit.YEARS);
            dateLastStr = dateLast.format(monthDate);
            String dateLastStr1=dateLast.minusMonths(1).format(monthDate);
            String dateLastStr2=dateLast.minusMonths(2).format(monthDate);
            String dateLastStr3=dateLast.minusMonths(3).format(monthDate);
            String dateLastStr4=dateLast.minusMonths(4).format(monthDate);
            String dateLastStr5=dateLast.minusMonths(5).format(monthDate);
            String dateLastStr6=dateLast.minusMonths(6).format(monthDate);
            String dateLastStr7=dateLast.minusMonths(7).format(monthDate);
            String dateLastStr8=dateLast.minusMonths(8).format(monthDate);
            String dateLastStr9=dateLast.minusMonths(9).format(monthDate);
            String dateLastStr10=dateLast.minusMonths(10).format(monthDate);
            String dateLastStr11=dateLast.minusMonths(11).format(monthDate);
            recentYearArr = new String[]{dateStr11,dateStr10,dateStr9,dateStr8,dateStr7,dateStr6,dateStr5,dateStr4,dateStr3,dateStr2,dateStr1,dateStr};
            recentYearCompareArr = new String[]{dateLastStr11,dateLastStr10,dateLastStr9,dateLastStr8,dateLastStr7,dateLastStr6,dateLastStr5,dateLastStr4,dateLastStr3,dateLastStr2,dateLastStr1,dateLastStr};
            dateStr="('"+dateStr+"','"+dateStr1+"','"+dateStr2+"','"+dateStr3+"','"+dateStr4+"','"+dateStr5+"','"+dateStr6+"','"+dateStr7+"','"+dateStr8+"','"+dateStr9+"','"+dateStr10+"','"+dateStr11+"')";
            dateLastStr="('"+dateLastStr+"','"+dateLastStr1+"','"+dateLastStr2+"','"+dateLastStr3+"','"+dateLastStr4+"','"+dateLastStr5+"','"+dateLastStr6+"','"+dateLastStr7+"','"+dateLastStr8+"','"+dateLastStr9+"','"+dateLastStr10+"','"+dateLastStr11+"')";
            paramDateLineBuilder.append("and to_char(t.bus_date, 'yyyy-MM') in " + dateStr + "");
            paramDateLineCompareBuilder.append("and to_char(t.bus_date, 'yyyy-MM') in " + dateLastStr + "");
            this.dateFormat="yyyy-MM";
        }else if(4 == lineType){
            //   4 按年--近5年
            // 本年
            dateStr = String.valueOf(LocalDate.now().getYear());
            // 去年同期
            dateLastStr= String.valueOf(LocalDate.now().getYear() - 1);
            paramDateLineBuilder.append("and to_char(t.bus_date, 'yyyy') in (" +  MyUtils.getRecentYear(dateStr) + ")");
            paramDateLineCompareBuilder.append("and to_char(t.bus_date, 'yyyy') in (" +  MyUtils.getRecentYear(dateLastStr) + ")");
            recentYearArr = MyUtils.getRecentYearArr(dateStr);
            recentYearCompareArr = MyUtils.getRecentYearArr(dateLastStr);
            this.dateFormat="yyyy";
        }
    }

    public String getConditionAreaStr() {
        if (!StringUtils.hasText(this.sgbCode)) {
            // 如果前端页面没有给传递区划编号，那么就查询当前登录人所在区划下的所有区划
            String sgbCode = ThreadLocalUtil.get().getBody().getAreainfo().getCode();
            paramAreaBuilder.append(" and t.GBCODE like '" + sgbCode + "%' ");
        }
        return paramAreaBuilder.toString();
    }

    public String getConditionDateStr() {
        return paramDateBuilder.toString();
    }

    public String getConditionCompareDateStr() {
        return paramDateCompareBuilder.toString();
    }
    public String getConditionLineDateStr() {
        return paramDateLineBuilder.toString();
    }

    public String getConditionLineCompareDateStr() {
        return paramDateLineCompareBuilder.toString();
    }

    public String getStartDate() { return startDate; }

    public String getEndDate() { return endDate; }

    public String getSgbCode() {
        return sgbCode;
    }

    public String getTargetType() {
        return targetType;
    }

    public Integer getDateType() {
        return dateType;
    }

    public boolean getCompare() {
        return compare;
    }

    public void setCompare(boolean compare) {
        this.compare = compare;
    }

    public boolean isCompare() {
        return compare;
    }

    public String[] getTargetTypeArr() {
        return targetTypeArr;
    }

    public void setTargetTypeArr(String[] targetTypeArr) {
        this.targetTypeArr = targetTypeArr;
    }

    public String[] getRecentYearArr() { return recentYearArr; }

    public void setRecentYearArr(String[] recentYearArr) { this.recentYearArr = recentYearArr; }

    public String[] getRecentYearCompareArr() { return recentYearCompareArr; }

    public void setRecentYearCompareArr(String[] recentYearCompareArr) { this.recentYearCompareArr = recentYearCompareArr; }

    public String getInfectionCode() {
        return infectionCode;
    }

    public void setInfectionCode(String infectionCode) {
        String codes="";
        if(StringUtils.hasText(infectionCode)){
            String str = "";
            String[] infectionCodeArr = infectionCode.split(",");
            for(int i=0;i<infectionCodeArr.length;i++){
                if (i>0){
                    str += ",";
                }
                str += "'"+infectionCodeArr[i]+"'";
            }
            if (str.length() >0){
                codes = "and t.SPARE_CODE in ("+str+")";
            }
        }
        this.infectionCode = codes;
    }

    public Integer getLineType() {
        return lineType;
    }

    public String getDateFormat() {
        return dateFormat;
    }

    public void setDateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
    }
}
